<template>
  <div>
    <el-upload
      class="upload-demo"
      ref="upload"
      :action="url"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :file-list="fileList"
      :auto-upload="false"
      :on-success="successUpload"
    >
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button
        style="margin-left: 10px"
        size="small"
        type="success"
        @click="submitUpload"
        >上传到服务器</el-button
      >
    </el-upload>
  </div>
</template>

<script>
import base from "../../api/base.js";
export default {
  data() {
    return {
      url: base.uploadUrl, //图片地址服务器
      fileList: [],
    };
  },
  methods: {
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    successUpload(response, file, fileList) {
      console.log(response, file, fileList);
      this.$message({
        message: "图片上传成功",
        type: "success",
      });
      //把成功的数据接口  response传递给父组件显示
      let imgUrl = base.host + "/" + response.url.slice(7);
      this.$emit('sendImg',imgUrl)
    },
  },
};
</script>

<style></style>
